package hero.antlr;

import org.antlr.runtime.Token;
import org.antlr.runtime.tree.TreeAdaptor;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.TokenRewriteStream;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;

public class Test {

    public static void main(String[] args) {
        test2();
    }


    public static void test1() {
        try {
            JavaLexer lex = new JavaLexer(new ANTLRFileStream("Test.java"));
            CommonTokenStream tokens = new CommonTokenStream(lex);
            JavaParser parser = new JavaParser(tokens);
            parser.compilationUnit();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void test2() {
        try {
            TreeAdaptor adaptor = new CommonTreeAdaptor() {
                public Object create(Token payload) {
                    if (payload ==null ) {
                        //System.out.println("meet null token" );
                    } else {
                        System.out.println("meet token " + payload.getText());
                    }
                    if (payload !=null && payload.getText().equals("DOC")) {
                        return new HtmlDocNode(payload);
                    } else  if (payload !=null && payload.getText().equals("SHIT")) {
                        return new HtmlBodyNode(payload);
                    }
                    return new CommonTree(payload);
                }
            };

            ANTLRFileStream fs = new ANTLRFileStream("test.html");
            HtmlDocLexer lex = new HtmlDocLexer(fs);
            TokenRewriteStream tokens = new TokenRewriteStream(lex);
            HtmlDocParser parser = new HtmlDocParser(tokens);

            parser.setTreeAdaptor(adaptor);
            HtmlDocParser.html_doc_return ret = parser.html_doc();
            CommonTree tree = (CommonTree)ret.getTree();
            printTree(tree,0);
            
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
    }

    public static void printTree(CommonTree t, int indent) {
        if ( t != null ) {
            StringBuffer sb = new StringBuffer(indent);
            for ( int i = 0; i < indent; i++ )
                sb = sb.append("   ");
            for ( int i = 0; i < t.getChildCount(); i++ ) {
                System.out.println(sb.toString() + t.getChild(i).toString());
                printTree((CommonTree)t.getChild(i), indent+1);
            }
        }
    }
    

}

class HtmlDocNode extends CommonTree {

    public HtmlDocNode() {
        super();
    }

    public HtmlDocNode(CommonTree node) {
        super(node);
    }
    public HtmlDocNode(Token t) {
        super(t);
    }
    public String toString() {
        return "you got a html docNode, node text is " + getText();
    }

}

class HtmlBodyNode extends CommonTree {

    public HtmlBodyNode() {
        super();
    }

    public HtmlBodyNode(CommonTree node) {
        super(node);
    }
    public HtmlBodyNode(Token t) {
        super(t);
    }

    public String toString() {
        return "you got a html docNode, node text is " + getText();
    }

}
